<?php
//StudentAction.class.php  学生相关操作
if(!defined("Yourphp")) exit("Access Denied");
class StudentAction extends BaseAction
{
    function _initialize()
    {	
		parent::_initialize();
		if(!$this->_userid){
			$this->assign('jumpUrl','http://user.fjhgkj.com.cn/index.php?u=jzzy');
			$this->error(L('nologin'));
			exit;
        }
    }
    //我的考试
    public function index(){
        $my_list = M('exam_record')->where(array('user_id'=>$this->_userid,'type'=>0))->select();
        $all_list = M('exam_record')->where(array('type'=>0))->select();
        foreach($my_list as $k=>$v){
            foreach($all_list as $ke=>$ve){
                if($my_list[$k]['eid'] == $all_list[$ke]['eid']){
                $my_list[$k]['total_num'] = M('exam_record')->where(array('eid'=>$my_list[$k]['eid'],'type'=>0))->count();
                $nofinish = M('exam_record')->where(array('eid'=>$my_list[$k]['eid'],'type'=>0,'status'=>0))->count();
                $my_list[$k]['finish_num'] = $my_list[$k]['total_num'] - $nofinish;
                $my_list[$k]['time'] = M('exam')->where(array('id'=>$my_list[$k]['eid']))->getField('totaltime');
                $my_list[$k]['title'] = M('exam')->where(array('id'=>$my_list[$k]['eid']))->getField('title');
            }
            }
        }
        $this->assign('my_list',$my_list);
        $this->display();
    }
    //学生考试页面
    public function my_exam(){
        //question_type:1选择题single  2多选题multi  3判断题judge  4填空题fill  5主观题subjective
        $exam = M('exam')->where(array('id'=>$_GET['eid']))->find();
        $exam_question = json_decode($exam['question'],true)['que'];
        $exam_score = json_decode($exam['question'],true)['sco'];
        foreach($exam_question as $k=>$v){
            $data[$k] = M('question_bank')->where(array('id'=>$v))->find(); 
        }
        foreach($data as $k=>$v){
            if($v['type']==1 || $v['type']==2 || $v['type']==3){
                $arr = trim($v['answeroptions'],"|");
                $option = explode("|",$arr);
                foreach($option as $ke=>$ve){
                  $key_name = strstr($ve,".",true);
                  $v['select'][$key_name] = $ve;
                }
            }        
           switch($v['type']){
                case 1:$single[$k] = $v;break;
                case 2:$multi[$k] = $v;break;
                case 3:$judge[$k] = $v;break;
                case 4:$fill[$k] = $v;break;
                case 5:$subjective[$k] = $v;break;
            }
        }
        $this->assign('eid',$_GET['eid']);
        $this->assign('exam_score',$exam_score);
        $this->assign('single',$single);
        $this->assign('multi',$multi);
        $this->assign('judge',$judge );
        $this->assign('fill',$fill);
        $this->assign('subjective',$subjective);
        $record = M('exam_record')->where(array('eid'=>$_GET['eid'],'user_id'=>$this->_userid,'username'=>$this->_username,'type'=>0))->find();
        if($record['status'] != 0){
            echo "<script>window.alert('已参与过该考试');history.go(-1);</script>";die;
        }
        if($record['end_time'] != ''){
            $end_time = $record['end_time'];
            if($end_time){
                $now = strtotime('now');
                $second = floor($end_time - $now);
                $this->assign('second',$second);
                $this->display();
           }
           else{
            echo "<script>window.alert('访问错误1');history.go(-1);</script>";die;
           }
        }else{
            $totaltime = M('exam')->where(array('id'=>$_GET['eid']))->getField('totaltime');
            $data = array('start_time'=>strtotime('now'),'end_time'=>strtotime('+'.$totaltime.'minute'));
            // $do = M('exam_record')->add($data);
            $do = M('exam_record')->where(array('eid'=>$_GET['eid'],'user_id'=>$this->_userid,'username'=>$this->_username))->save($data);
            if($do){
                $second = floor($totaltime * 60);
                $this->assign('second',$second);
                $this->display();
            }else{
                echo "<script>window.alert('访问错误2');history.go(-1);</script>";die;
            }
        }
    }
    //考试结束
    public function finish_exam(){
        if(IS_POST){
            $map = array('eid'=>$_GET['eid'],'user_id'=>$this->_userid);
            $end_time = M('exam_record')->where($map)->getField('end_time');
            // $finish_time = strtotime('now');
            if(!$end_time){
                echo "<script>window.alert('答卷人与试卷不匹配');history.go(-2);</script>";die;
            }
            else{
                if($_FILES['upfile']['size'] != 0){
                    $upload_path = $this->upload($_FILES);
                    $arr = array('answers'=>json_encode($_POST,true),'zg_uploads'=>$upload_path,'status'=>1);
                }else{
                    $arr = array('answers'=>json_encode($_POST,true),'status'=>1);
                }
                $exam_record = M('exam_record')->where($map)->save($arr);
                if($exam_record){
                    echo "<script>window.alert('保存成功');history.go(-2);</script>";die;
                }
            }      
        }
    }
    //我的作业列表
    public function homework_list(){
        $my_list = M('exam_record')->where(array('user_id'=>$this->_userid,'type'=>1))->select();
        $all_list = M('exam_record')->where(array('type'=>1))->select();
        foreach($my_list as $k=>$v){
            foreach($all_list as $ke=>$ve){
                if($my_list[$k]['eid'] == $all_list[$ke]['eid']){
                $my_list[$k]['total_num'] = M('exam_record')->where(array('eid'=>$my_list[$k]['eid'],'type'=>0))->count();
                $nofinish = M('exam_record')->where(array('eid'=>$my_list[$k]['eid'],'type'=>0,'status'=>0))->count();
                $my_list[$k]['finish_num'] = $my_list[$k]['total_num'] - $nofinish;
                $my_list[$k]['time'] = date("Y-m-d H:i:s",M('exam')->where(array('id'=>$my_list[$k]['eid']))->getField('tasktime'));
                $my_list[$k]['title'] = M('exam')->where(array('id'=>$my_list[$k]['eid']))->getField('title');
            }
            }
        }
        // var_dump($all_list);
        $this->assign('my_list',$my_list);
        $this->display();
    }
    //我的作业页面
    public function homework(){
    //question_type:1选择题single  2多选题multi  3判断题judge  4填空题fill  5主观题subjective
        $exam = M('exam')->where(array('id'=>$_GET['eid']))->find();
        $exam_question = json_decode($exam['question'],true)['que'];
        $exam_score = json_decode($exam['question'],true)['sco'];
        foreach($exam_question as $k=>$v){
            $data[$k] = M('question_bank')->where(array('id'=>$v))->find(); 
        }
        foreach($data as $k=>$v){
            if($v['type']==1 || $v['type']==2 || $v['type']==3){
                $arr = trim($v['answeroptions'],"|");
                $option = explode("|",$arr);
                foreach($option as $ke=>$ve){
                    $key_name = strstr($ve,".",true);
                    $v['select'][$key_name] = $ve;
                }
            }        
            switch($v['type']){
                case 1:$single[$k] = $v;break;
                case 2:$multi[$k] = $v;break;
                case 3:$judge[$k] = $v;break;
                case 4:$fill[$k] = $v;break;
                case 5:$subjective[$k] = $v;break;
                }
            }
        $now = strtotime('now');
        $tasktime = M('exam')->where(array('id'=>$_GET['eid']))->getField('tasktime');
        if($now-$tasktime>0){
            echo "<script>window.alert('作业已经截止');history.go(-1);</script>";
            die;
        }
        $record = M('exam_record')->where(array('eid'=>$_GET['eid'],'user_id'=>$this->_userid,'username'=>$this->_username,'type'=>1))->find();
        if($record['status'] != 0){
            echo "<script>window.alert('已提交过该份作业');history.go(-2);</script>";die;
        }
        $data = array('end_time'=>$tasktime,'eid'=>$_GET['eid'],'user_id'=>$this->_userid,'username'=>$this->_username);
        $do = M('exam_record')->where(array('eid'=>$_GET['eid'],'user_id'=>$this->_userid,'username'=>$this->_username))->save($data);
        $time = date("Y-m-d H:i:s",$tasktime);
        $this->assign('tasktime',$time);
        $this->assign('eid',$_GET['eid']);
        $this->assign('exam_score',$exam_score);
        $this->assign('single',$single);
        $this->assign('multi',$multi);
        $this->assign('judge',$judge );
        $this->assign('fill',$fill);
        $this->assign('subjective',$subjective);
        $this->display();
    }
    // //作业暂存
    // public function temporary(){
    //     if(IS_POST){
    //         $map = array('eid'=>$_GET['eid'],'user_id'=>$this->_userid);
    //         $tasktime = M('exam')->where(array('id'=>$_GET['eid']))->getField('tasktime');
    //         $now = strtotime('now');
    //         if($now-$tasktime>0){
    //             echo "<script>window.alert('未按时完成作业');history.go(-2);</script>";die;
    //         }else{
    //             if($_FILES){
    //                 $upload_path = $this->upload($_FILES);
    //                 // $arr = array('answers'=>explode('\r\n',json_encode($_POST)),'zg_uploads'=>$upload_path);
    //                 $arr = array('answers'=>explode('\r\n',json_encode($_POST)),'zg_uploads'=>$upload_path);
    //             }else{
    //                 $arr = array('answers'=>explode('\r\n',json_encode($_POST));
    //             }
    //             $exam_record = M('exam_record')->where($map)->save($arr);
    //             if($exam_record){
    //                 echo "<script>window.alert('保存成功');history.go(-2);</script>";die;
    //             }
    //         }
    //         }
    // }
    //完成作业
    public function finish_homework(){
        if(IS_POST){
            $map = array('eid'=>$_GET['eid'],'user_id'=>$this->_userid);
            $tasktime = M('exam')->where(array('id'=>$_GET['eid']))->getField('tasktime');
            $now = strtotime('now');
            if($now-$tasktime >0){
                echo "<script>window.alert('未按时完成作业');history.go(-1);</script>";die;
            }else{
                if($_FILES['upfile']['size'] != 0){
                    $upload_path = $this->upload($_FILES);
                    $arr = array('answers'=>json_encode($_POST,true),'zg_uploads'=>$upload_path,'status'=>1);
                }else{
                    $arr = array('answers'=>json_encode($_POST,true),'status'=>1);
                }
                $exam_record = M('exam_record')->where($map)->save($arr);
                if($exam_record){
                    echo "<script>window.alert('提交成功');history.go(-2);</script>";die;
                }
            }
            }
    }
    //点赞他人作业
    public function laud(){
        $map = array('user_id'=>$this->_userid,'exid'=>$_GET['id']);
        $laud = M('laud')->where($map)->find();
        if($laud){
            $zan = M('exam_record')->where(array('id'=>$_GET['id']))->setDec('laud',1);
            $del = M('laud')->where($map)->delete();
            $zan_num = M('exam_record')->where(array('id'=>$_GET['id']))->getField('laud');
            $this->ajaxReturn($zan_num);
        }else{
            $zan = M('exam_record')->where(array('id'=>$_GET['id']))->setInc('laud',1);
            $add = M('laud')->add($map);
            $zan_num = M('exam_record')->where(array('id'=>$_GET['id']))->getField('laud');
            $this->ajaxReturn($zan_num);
        }
    }
    //返回分享链接
    public function do_share(){
        $path = $this->upload($_FILES);
        $arr = array('user_id'=>$this->_userid,'img'=>$path,'said'=>$_POST['said'],'exid'=>$_GET['id'],'username'=>$this->_username);
        $do = M('share')->add($arr);
        if($do){
            $url = "http://".$_SERVER['SERVER_NAME'].'/index.php?g=User&m=Share&a=index&id='.$do;
            $this->ajaxReturn($url);
        }
    }
    //学生上传附件
    public function upload($files){
        $new_name = strtotime('now').rand(1,1000);
        $houzui = explode('.',$files['upfile']['name']);
        // print_r($houzui);exit;
        $name = $new_name.'.'.$houzui[1];
        if(move_uploaded_file($files['upfile']['tmp_name'],UPLOAD_PATH.$name)){  
            $upload_path = '/Uploads/'.$name;
            // $path = iconv("gb2312","UTF-8",$upload_path);
            return $upload_path;
        }
        else{
            echo "<script>window.alert('上传失败，请重新传');history.go(-1);</script>";die;
        }
    }
    //已学习的岗位和项目
    public function visited() {
        $list = M('readrecord')->query("select fi.* from yourphp_readrecord as r left join yourphp_f_item as fi on r.fid = fi.id where r.userid = '$this->_userid' and r.groupid = '".cookie('groupid')."' order by r.id desc ");
        foreach ($list as $key => $value) {
            $list[$key]['renwus'] = M('renwu')->where(array('xmid' => $value['id']))->count();
            $list[$key]['weikes'] = M('weike')->where(array('xmid' => $value['id']))->count();
            $list[$key]['scbs'] = M('scb')->where(array('xmid' => $value['id']))->count();
            $list[$key]['zyid'] = M('gangwei')->where(array('id'=>$value['gwid']))->getField('zyid') ;
        }


        $gangwei = M('readrecord')->query("select fi.id from yourphp_readrecord as r left join yourphp_f_item as fi on r.fid = fi.id where r.userid = '$this->_userid' and r.groupid = '".cookie('groupid')."' group by fi.gwid ");

        $this->assign('list', $list);
        $this->assign('gwsl', count($gangwei));
        $this->assign('yxxxm', count($list));
        $this->display();
    }
    //我的收藏
    public function collect() {
        $list = M('readrecord')->query("select fi.id from yourphp_readrecord as r left join yourphp_f_item as fi on r.fid = fi.id where r.userid = '$this->_userid' and r.groupid = '".cookie('groupid')."' ");  //项目
        $gangwei = M('readrecord')->query("select fi.id from yourphp_readrecord as r left join yourphp_f_item as fi on r.fid = fi.id where r.userid = '$this->_userid' and r.groupid = '".cookie('groupid')."' group by fi.gwid "); //岗位
        $this->assign('gwsl', count($gangwei));
        $this->assign('yxxxm', count($list));
        $data = array();
        $jxsp = M('videocollect')->where(array('uid' => $this->_userid,'groupid'=>cookie('groupid')))->order('id desc')->select();
        foreach ($jxsp  as $kj => $vj) {
            if ($vj['type'] == 'rwid') {
                $data['jxsp'][$kj] = $vj;
            }
            if ($vj['type'] == 'wkid') {
                $data['wk'][$kj] = $vj;
            }
            if ($vj['type'] == 'kjid') {
                $data['dzkj'][$kj] = $vj;
            }
            if ($vj['type'] == 'dzjc') {
                $data['dzjc'][$kj] = $vj;
            }
        }

         $this->assign('data', $data);
        $this->display();
    }

    //我的资料
    public function mydata() {
        $actionUrl = "http://user.fjhgkj.com.cn/index.php?m=home&c=index&a=edit";
        $url=$_SERVER["REQUEST_SCHEME"].'://'.$_SERVER["SERVER_NAME"].':'.$_SERVER["SERVER_PORT"].U('Home/Index/index');
        $arr = ['id'=>$this->_userid,'type'=>$this->_ms,'url'=>$url];
        $formItemString='';
        foreach($arr as $key=>$value){
            $formItemString.="<input name='{$key}' type='text' value='{$value}'/>";
        }
        $content=<<<EOT
        <form style='display:none' name='submit_form' id='submit_form' action='{$actionUrl}' method='post'>
        {$formItemString}
        </form>
        <script type="text/javascript">
        document.submit_form.submit();
        </script>
EOT;
        echo $content;
        // header("Location: http://user.fjhgkj.com.cn/index.php?m=home&c=index&a=edit_zy&id=".$this->_userid."&type=".$this->_groupid."&url=".urlencode($_GET['url'])) ;
    }

}